﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using HHCommon;

namespace HHWeb.SystemManage.NewClass
{
    public partial class Dispose : Base
    {
        Help help = new Help();
        EncAndDec EncAndDec = new EncAndDec();
        HHCommon.PagePower  pagepower= new HHCommon.PagePower();
        HHBLL.NewClass B= new HHBLL.NewClass();
        HHModel.NewClass M= new HHModel.NewClass();
        //变量定义
        public string NewClassName = string.Empty;
        public string ParentID =string.Empty;
        public string Remark = string.Empty;
        public int Sort =0;
        public int Status =1;
        public string Creater = string.Empty;
        public string CreateTime = string.Empty;
        public string ParentInfo = string.Empty;
        public string Command = "Add";//执行命令
        protected void Page_Load(object sender, EventArgs e)
        {
            string message = string.Empty;
            try
            {
                //判断页面提交方式：是POST还是GET
                if (help.GetHttpMethodBool())
                {
                    Command = Request.Form["Command"].ToString();
                    switch (Command)
                    {
                        case "Add":
                            message = AddNewClass();
                            break;
                        case "Update":
                            message = UpdateNewClass();
                            break;
                        default:
                            break;
                    }
                    if (message.EndsWith("成功！"))
                        MessageBox.ShowAndRedirect(this.Page, message, "List.aspx");
                    else
                        MessageBox.ResponseScript(this.Page, message, "javascript:history.go(-1)");
                }
                else
                {
                    if (help.IFPageParameter())
                    {
                        int NewClassID = Convert.ToInt32(Request.QueryString["ID"].ToString());
                        M = B.GetModel(NewClassID);
                        if (M != null)
                        {
                            NewClassName = M.NewClassName;
                            ParentID = M.ParentID.ToString();
                            Remark = M.Remark;
                            Sort = Convert.ToInt32(M.Sort);
                            Status = Convert.ToInt32(M.Status);
                            Creater = M.Creater;
                            CreateTime = Convert.ToDateTime(M.CreateTime).ToString("yyyy-MM-dd HH:mm;ss");
                            Command = "Update";
                        }
                        else
                        {
                            MessageBox.ShowAndRedirect(this.Page, "该数据不存在！", "List.aspx");
                            return;
                        }
                    }
                    else
                    {
                        Creater = EncAndDec.DESDecrypting(help.GetCookie("UserCode"));
                        CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
                //新闻类别（无限极下拉框）
                GetParentInfo();
            }
            catch
            {
                MessageBox.ResponseScript(this.Page, "操作错误，请联系管理员！", "javascript:history.go(-1)");
                return;
            }
        }

        #region //新闻类别（无限极下拉框）
        public void GetParentInfo()
        {
            ParentInfo = pagepower.GetPagePowerInfo("Select [ID] as ID ,[NewClassName] as Name,[ParentID] as ParentID from NewClass with(nolock) where [Status]=1 order by sort desc");
        }
        #endregion

        #region //添加新闻类别
        public string AddNewClass()
        {
            M.NewClassName = Request.Form["NewClassName"].ToString();
            M.ParentID = Convert.ToInt32(Request.Form["ParentID"].ToString());
            M.Remark = Request.Form["Remark"].ToString();
            M.Sort = Convert.ToInt32(Request.Form["Sort"].ToString());
            M.Status = Convert.ToInt32(Request.Form["Status"].ToString());
            M.Creater = Request.Form["Creater"].ToString();
            M.CreateTime = Convert.ToDateTime(Request.Form["CreateTime"]);
            if (B.Add(M)> 0)
                return "添加成功！";
            else
                return "添加失败！";
        }
        #endregion

        #region //修改新闻类别
        public string UpdateNewClass()
        {
            M.ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
            M.NewClassName = Request.Form["NewClassName"].ToString();
            M.ParentID = Convert.ToInt32(Request.Form["ParentID"].ToString());
            M.Remark = Request.Form["Remark"].ToString();
            M.Sort = Convert.ToInt32(Request.Form["Sort"].ToString());
            M.Status = Convert.ToInt32(Request.Form["Status"].ToString());
            M.Creater = Request.Form["Creater"].ToString();
            if (B.Update(M))
                return "修改成功！";
            else
                return "修改失败！";
        }
        #endregion
    }
}